package com.huang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huang.yygh.cmn.client.DictFeignClient;
import com.huang.yygh.enums.DictEnum;
import com.huang.yygh.hosp.repository.HospitalRepository;
import com.huang.yygh.hosp.service.HospitalService;
import com.huang.yygh.model.hosp.Hospital;
import com.huang.yygh.vo.hosp.HospitalQueryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HuangShen
 * @description: HospitalService业务层实现类
 * @date 2021/5/3 21:42
 */
@Service
public class HospitalServiceImpl implements HospitalService {


    private static final Logger LOGGER = LoggerFactory.getLogger(HospitalServiceImpl.class);

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;


    @Override
    public void save(Map<String, Object> paramMap) {
        LOGGER.info("上传医院信息{}", JSONObject.toJSONString(paramMap));
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (null != targetHospital) {
            hospital.setId(targetHospital.getId());
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 查询医院
     *
     * @param hoscode 医院编号
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {


        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 分页查询
     *
     * @param page            当前页码
     * @param limit           每页记录数
     * @param hospitalQueryVo 查询条件
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        PageRequest pageRequest = PageRequest.of(page-1, limit, sort);


        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        hospital.setIsDeleted(0);
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        Page<Hospital> hospitals = hospitalRepository.findAll(example, pageRequest);
        hospitals.getContent().forEach(this::packHospital);



        return hospitals ;
    }

    /**
     * 更新上线状态
     *
     * @param id     医院ID
     * @param status 状态
     */
    @Override
    public void updateStatus(String id, Integer status) {
        if(status == 0 || status == 1) {
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }



    }

    /**
     * 获取医院详情
     *
     * @param id 医院id
     * @return
     */
    @Override
    public Map<String, Object> show(String id) {
        Map<String, Object> result = new HashMap<>();

        Hospital hospital = this.packHospital(this.hospitalRepository.findById(id).get());
        result.put("hospital", hospital);

        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 根据医院编号获取医院名称接口
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";

    }


    /**
     * 封装数据
     * @param hospital
     * @return
     */
    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(),hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
        return hospital;
    }




}
